package cn.ike.service.cargo.impl;

import cn.ike.dao.cargo.ContractDao;
import cn.ike.dao.cargo.ExtCproductDao;
import cn.ike.domain.cargo.Contract;
import cn.ike.domain.cargo.ExtCproduct;
import cn.ike.domain.cargo.ExtCproductExample;
import cn.ike.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    // 附件
    @Autowired
    private ExtCproductDao extCproductDao;
    // 购销合同
    @Autowired
    private ContractDao contractDao;


    /**
     * 分页查询
     * @param extCproductExample
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
        return new PageInfo<>(list);
    }

    /**
     * 查询所有
     * @param extCproductExample
     */
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        return extCproductDao.selectByExample(extCproductExample);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param extCproduct
     */
    @Override
    public void save(ExtCproduct extCproduct) {
        extCproduct.setId(UUID.randomUUID().toString());
        // 计算金额
        Double aumount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null){
            aumount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(aumount);
        extCproductDao.insertSelective(extCproduct);

        // 更改购销合同
        // a.查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        // b.设置总金额 = 总金额 + 附件总金额
        contract.setTotalAmount(contract.getTotalAmount() + aumount);
        // c.设置附件数量 = 附件总数 + 1
        contract.setExtNum(contract.getExtNum() + 1);
        // d.保存购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 修改
     * @param extCproduct
     */
    @Override
    public void update(ExtCproduct extCproduct) {
        // 修改后附件金额
        Double Amount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null){
            Amount = extCproduct.getPrice() * extCproduct.getCnumber();
        }
        // 更新附件
        extCproduct.setAmount(Amount);
        // 获取修改前的附件金额
        ExtCproduct extCproduct1 = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Double oldAmount = extCproduct1.getAmount();

        extCproductDao.updateByPrimaryKeySelective(extCproduct);

        // 更新合同
        // a.查询合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        // b.更新合同金额 = 合同总金额 + 修改后的附件金额 - 修改前的附件金额
        contract.setTotalAmount(contract.getTotalAmount() + Amount - oldAmount);
        // 更新het
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id) {
        // 查询合同id
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        String contractId = extCproduct.getContractId();
        // 删除附件
        extCproductDao.deleteByPrimaryKey(id);
        // 更新合同
        // a.查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        // c.更新合同附件数量
        contract.setExtNum(contract.getExtNum() - 1);
        // 更新
        contractDao.updateByPrimaryKeySelective(contract);
    }
}
